package com.yc.utils;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;

public class ControlUtil {
	
	public static void circleControl(Button button) {
		Region region = new Region();
		region.add(circle(50, 52, 52));
		button.addPaintListener(new PaintListener(){
			@Override
			public void paintControl(PaintEvent arg0) {
				button.setRegion(region);
			}
		});
	}
	
	static int[] circle(int r, int offsetX, int offsetY) {
        int[] polygon = new int[8 * r + 4];

        // x^2 + y^2 = r^2
        for (int i = 0; i < 2 * r + 1; i++) {
            int x = i - r;
            int y = (int)Math.sqrt(r * r - x * x);
            polygon[2*i] = offsetX + x;
            polygon[2*i+1] = offsetY + y;
            polygon[8*r - 2*i - 2] = offsetX + x;
            polygon[8*r - 2*i - 1] = offsetY - y;
        }
        return polygon;

    }
}
